<template>
  <div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" @close="closeDialog" :destroy-on-close="true" :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="menuForm" :rules="menuRules" ref="menuForm" :inline="true">
        <el-form-item label="父级菜单" :label-width="formLabelWidth">
          <el-input v-model="menuForm.parentName" autocomplete="off" :readonly="true" @click.native="openTreeDialog"></el-input>
        </el-form-item>
        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="menuName">
          <el-input v-model="menuForm.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-show="menuForm.menuType !== '2'" label="菜单路径" :label-width="formLabelWidth" prop="menuPath">
          <el-input v-model="menuForm.menuPath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" :label-width="formLabelWidth" prop="menuIcon">
          <i id="selectedIcon" :class="menuForm.menuIcon" @click="iconDialogVisible = true" v-if="menuForm.menuIcon"></i>
          <el-button v-else @click="openIconDialog">选择图标</el-button>
        </el-form-item>
        <el-form-item v-show="menuForm.menuType !== '2'" label="路由名称" :label-width="formLabelWidth" prop="routerName">
          <el-input v-model="menuForm.routerName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-show="menuForm.menuType !== '2'" label="路由组件" :label-width="formLabelWidth" prop="routerComponent">
          <el-input v-model="menuForm.routerComponent" autocomplete="off" aria-placeholder="例如：User"></el-input>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth" prop="menuPermission">
          <el-input v-model="menuForm.permission" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单排序" :label-width="formLabelWidth" prop="orderNum">
          <el-input id="sb" v-model="menuForm.orderNum" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item label="菜单状态" :label-width="formLabelWidth" prop="status">
          <el-radio-group v-model="menuForm.status">
            <el-radio label="0">激活</el-radio>
            <el-radio label="1">隐藏</el-radio>
            <el-radio label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth" prop="menuType">
          <el-radio-group v-model="menuForm.menuType">
            <el-radio label="0">目录</el-radio>
            <el-radio label="1">菜单</el-radio>
            <el-radio label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addOrEditMenu">确 定</el-button>
      </div>
      <TreeDialog></TreeDialog>
      <IconDialog></IconDialog>
    </el-dialog>


  </div>
</template>

<script>
import {addMenu, getMenus, editMenu} from "@/api/menu";
import {Message} from "element-ui";
import TreeDialog from "@/components/dialog/TreeDialog";
import IconDialog from "@/components/dialog/IconDialog";

export default {
  name: "MenuDialog",
  components: {
    TreeDialog,IconDialog
  },
  data() {
    return {
      iconDropdownMenus: [],
      formLabelWidth: '120px',
      menuRules: {
        parentName: [
          { required: true, message: '请选择父级菜单', trigger: 'blur' }
        ],
        menuName: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        menuPath: [
          { required: this.showInputs, message: '请输入菜单对应路径', trigger: 'blur' }
        ],
        routerName: [
          { required: this.showInputs, message: '请输入菜单路由名称', trigger: 'blur' }
        ],
        routerComponent: [
          { required: this.showInputs, message: '请输入菜单路由组件', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择菜单状态', trigger: 'change' }
        ],
        menuType: [
          { required: true, message: '请选择菜单类型', trigger: 'change' }
        ]
      }
    };
  },
  computed: {
    iconDialogVisible: {
      get() {
        return this.$store.state.menu.iconDialogVisible;
      },
      set(value) {
        this.$store.state.menu.iconDialogVisible = value;
      }
    },
    selectedIcon: {
      get() {
        return this.$store.state.menu.selectedIcon;
      },
      set(value) {
        this.$store.state.menu.selectedIcon = value;
      }
    },
    dialogFormVisible: {
      get() {
        return this.$store.state.menu.dialogFormVisible;
      },
      set(value) {
        this.$store.state.menu.dialogFormVisible = value;
      }
    },
    dialogTitle() {
      return this.$store.state.menu.dialogTitle;
    },
    menuForm: {
      get() {
        return this.$store.state.menu.menuForm;
      },
      set(value) {
        this.$store.state.menu.menuForm = value;
        this.selectedIcon = this.menuForm.menuIcon;
      }
    }
  },
  methods: {
    setClose() {
      this.iconDialogVisible = false;
    },
    setIcons() {

    },
    selectIcon(icon) {
      this.menuForm.menuIcon = icon;
      this.selectedIcon = icon;
      this.iconDialogVisible = false;
    },
    openIconDialog() {
      this.iconDialogVisible = true;
    },
    refreshData() {
      var params = {
        "current": this.$store.state.menu.currentPage,
        "size": this.$store.state.menu.pageSize
      };
      // 刷新
      getMenus(params).then((res) => {
        this.$store.state.menu.tableData = res.data.data;
        this.$store.state.menu.total = res.data.total;
        this.clearForm();
      })
    },
    clearForm() {
      Object.keys(this.menuForm).forEach((key) => {
        this.menuForm[key] = null;
      })
    },
    closeDialog() {
      this.$store.commit("menu/closeDialogForm", false);
      this.clearForm();
    },
    addOrEditMenu() {
      this.$refs["menuForm"].validate((valid) => {
        if (valid) {
          if (this.dialogTitle === "添加菜单") {
            addMenu(this.menuForm).then((res) => {
              Message.success(res.data.msg);
              this.dialogFormVisible = false;
              this.refreshData();
            })
          } else {
            editMenu(this.menuForm).then((res) => {
              Message.success(res.data.msg);
              this.dialogFormVisible = false;
              this.refreshData();
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    openTreeDialog() {
      this.$store.dispatch("menu/openTreeDialog", true);
    }

  },
  mounted() {
  }

}
</script>

<style scoped>
/deep/.el-input__inner {
  padding-right: 0px !important;
}

#iconDialog i {
  font-size: 30px;
  padding: 10px;
  margin-right: 5px;
  margin-bottom: 5PX;
  border: 1px solid beige;
  transition: all 0.2s ease;
}
#iconDialog i:hover {
  cursor: pointer;
  transform: scale(1.1);
}
#selectedIcon {
  font-size: 30px;
  border: 1px solid beige;
}
#selectedIcon:hover {
  cursor: pointer;
}

</style>
